import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

class PerformanceIssuePage extends StatefulWidget {
  const PerformanceIssuePage({super.key});

  @override
  State<PerformanceIssuePage> createState() => _PerformanceIssuePageState();
}

class _PerformanceIssuePageState extends State<PerformanceIssuePage> {
  bool isProcessing = false;

  void _simulateLongTask() {
    setState(() {
      isProcessing = true;
    });

    // 耗时操作模拟：直接在主线程上进行长时间的计算
    for (int i = 0; i < 1000000000; i++) {
      // 模拟复杂运算
    }

    setState(() {
      isProcessing = false;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('卡顿模拟'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            PerformanceOverlay.allEnabled(),
            isProcessing
                ? const CircularProgressIndicator()
                : const SizedBox.shrink(),
            const SizedBox(height: 20),
            ElevatedButton(
              onPressed: isProcessing ? null : _simulateLongTask,
              child: const Text('开始长时间任务'),
            ),
          ],
        ),
      ),
    );
  }
}
